package com.gmrz.uaf.server;


import com.gmrz.uaf.protocol.v1.schema.FidoLog;
import com.gmrz.util.UUIDGenerator;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chentaikun on 16/12/27.
 */
public class ServerContext {
    private FidoLog fidoLog;
    private boolean perform = false;
    private boolean isBase64 = false;

    private static final ThreadLocal<ServerContext> contexts = new ThreadLocal<ServerContext>();

    public static ServerContext init(HttpServletRequest request){
        ServerContext serverContext = new ServerContext();
        FidoLog fidoLog = new FidoLog();
        fidoLog.setId(UUIDGenerator.generateID());
        fidoLog.setRemoteIP(request.getRemoteAddr());
        fidoLog.setLocalIP(request.getLocalAddr());
        serverContext.setFidoLog(fidoLog);
        contexts.set(serverContext);
        return serverContext;
    }

    public static ServerContext getServerContext(){
        return contexts.get();
    }

    public void destroy(){

        contexts.remove();
    }

    public FidoLog getFidoLog() {
        return fidoLog;
    }

    private void setFidoLog(FidoLog fidoLog) {
        this.fidoLog = fidoLog;
    }

    public boolean isPerform() {
        return perform;
    }

    public void setPerform(String appId) {
        if(null != appId && "9999".equals(appId.trim())){
            this.perform = true;
        }
    }

    public boolean isBase64() {
        return isBase64;
    }

    public void setBase64(boolean base64) {
        this.isBase64 = base64;
    }
}
